﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace AhCha.Fortunate.Common.Utility
{
    public class RandomAvatarGenerator
    {
        private static readonly Random _random = new Random();

        public static byte[] GenerateRandomAvatar(int size = 200)
        {
            // 创建正方形画布
            using (Bitmap bitmap = new Bitmap(size, size))
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;

                // 随机背景（纯色或渐变）
                if (_random.NextDouble() > 0.3)
                    DrawRandomSolidBackground(g, size);
                else
                    DrawRandomGradientBackground(g, size);

                // 添加3-5个随机形状
                int shapeCount = _random.Next(3, 6);
                for (int i = 0; i < shapeCount; i++)
                {
                    DrawRandomShape(g, size);
                }

                // 添加中心符号（可选）
                if (_random.NextDouble() > 0.4)
                {
                    DrawCenterSymbol(g, size);
                }

                using (MemoryStream ms = new MemoryStream())
                {
                    bitmap.Save(ms, ImageFormat.Png);
                    return ms.ToArray();
                }
            }
        }

        private static void DrawRandomSolidBackground(Graphics g, int size)
        {
            Color bgColor = Color.FromArgb(
                _random.Next(200, 255),
                _random.Next(200, 255),
                _random.Next(200, 255));

            using (SolidBrush brush = new SolidBrush(bgColor))
            {
                g.FillRectangle(brush, 0, 0, size, size);
            }
        }

        private static void DrawRandomGradientBackground(Graphics g, int size)
        {
            Color color1 = Color.FromArgb(
                _random.Next(200),
                _random.Next(200),
                _random.Next(200));

            Color color2 = Color.FromArgb(
                _random.Next(200),
                _random.Next(200),
                _random.Next(200));

            Rectangle rect = new Rectangle(0, 0, size, size);
            using (LinearGradientBrush brush = new LinearGradientBrush(
                rect, color1, color2, _random.Next(360), true))
            {
                g.FillRectangle(brush, rect);
            }
        }

        private static void DrawRandomShape(Graphics g, int size)
        {
            int shapeType = _random.Next(3); // 0=圆形, 1=矩形, 2=三角形
            int x = _random.Next(size / 4, size - size / 4);
            int y = _random.Next(size / 4, size - size / 4);
            int sizeFactor = _random.Next(20, 80);

            Color shapeColor = Color.FromArgb(
                _random.Next(100, 200),
                _random.Next(100, 200),
                _random.Next(100, 200));

            using (Pen pen = new Pen(shapeColor, _random.Next(3, 8)))
            using (SolidBrush brush = new SolidBrush(shapeColor))
            {
                switch (shapeType)
                {
                    case 0: // 圆形
                        g.DrawEllipse(pen,
                            x - sizeFactor / 2,
                            y - sizeFactor / 2,
                            sizeFactor,
                            sizeFactor);
                        g.FillEllipse(brush,
                            x - sizeFactor / 2,
                            y - sizeFactor / 2,
                            sizeFactor,
                            sizeFactor);
                        break;

                    case 1: // 矩形
                        int width = _random.Next(30, 80);
                        int height = _random.Next(30, 80);
                        g.DrawRectangle(pen,
                            x - width / 2,
                            y - height / 2,
                            width,
                            height);
                        g.FillRectangle(brush,
                            x - width / 2,
                            y - height / 2,
                            width,
                            height);
                        break;

                    case 2: // 三角形
                        Point[] points = {
                        new Point(x, y - sizeFactor/2),
                        new Point(x - sizeFactor/2, y + sizeFactor/2),
                        new Point(x + sizeFactor/2, y + sizeFactor/2)
                    };
                        g.DrawPolygon(pen, points);
                        g.FillPolygon(brush, points);
                        break;
                }
            }
        }

        private static void DrawCenterSymbol(Graphics g, int size)
        {
            // 随机选择符号类型
            char symbol = (char)_random.Next('A', 'Z' + 1);
            if (_random.NextDouble() > 0.5)
                symbol = (char)_random.Next('0', '9' + 1);

            // 符号样式
            Font font = new Font("Arial", 80, FontStyle.Bold);
            Color symbolColor = _random.NextDouble() > 0.5
                ? Color.Black
                : Color.White;

            using (SolidBrush brush = new SolidBrush(symbolColor))
            {
                SizeF symbolSize = g.MeasureString(symbol.ToString(), font);
                float x = (size - symbolSize.Width) / 2;
                float y = (size - symbolSize.Height) / 2;
                g.DrawString(symbol.ToString(), font, brush, x, y);
            }
        }
    }

}